<template>
	<view class="container u-flex-col u-row-center">

		<view class="title">社区防疫行程卡</view>
		<view class="sub-title">社区防疫行程卡</view>

		<image src="../static/image/card_type.png" class="card-type"></image>


		<view class="text-2">当前查询内容</view>
		<view class="mobile u-m-t-40" v-if="vuex_token&&mobile">{{showMobile(mobile)}}</view>
		<view class="text-3" v-else>点击“查询”进行获取</view>

		<view class="query-buttuon u-m-t-40 u-m-l-80 u-m-r-80">
			<button type="primary" @click="query" v-if="vuex_token" style="background-color: #2979ff;">查询</button>
			<button type="primary" @click="gouxuan" v-else-if="!vuex_token&&!checked"
				style="background-color: #2979ff;">查询</button>
			<button type="primary" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber" v-else
				style="background-color: #2979ff;">查询</button>
			<view class="u-flex u-m-t-12">
				<u-checkbox v-model="checked" :labelSize="22" :iconize="22">同意并授权平台查询本人在疫情期间14天内到访信息</u-checkbox>
				<!-- <view class="text-2 u-m-t-25">同意并授权平台查询本人在疫情期间14天内到访信息</view> -->
			</view>

		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				checked: false,
				communityId: -1,
				code: '',
				mobile: '',
				checkFrom: false
			}
		},
		onLoad(option) {
			let scene = option.scene;
			if (scene) {
				this.communityId = scene;
			}
			let vuex_token = this.$store.state.vuex_token;
			this.mobile = uni.getStorageSync("mobile");
			this.checked = this.mobile && vuex_token;
			this.getConfig();
		},
		methods: {
			showMobile(mobile) {
				return mobile.substring(0, 3) + "****" + mobile.substring(mobile.length - 4, mobile.length)
			},

			getConfig() {
				this.$u.get('app/config', {}).then(ret => {
					this.checkFrom = (ret.checkFrom == 1)
				});
			},
			appWechatLogin(code, wechatRet) {
				uni.showLoading();
				this.$u.post('app/wechat/login', {
					code: code,
					encryptedData: wechatRet.detail.encryptedData,
					iv: wechatRet.detail.iv
				}).then(ret => {
					uni.hideLoading();
					console.log(ret);
					uni.$u.vuex('vuex_token', ret.token);
					uni.setStorageSync("expires_in", ret.expire);
					uni.setStorageSync("mobile", ret.mobile);
					uni.setStorageSync("last_login_time", new Date().getTime());
					this.mobile = uni.getStorageSync("mobile");
					this.query()
				});
			},

			onGetPhoneNumber(e) {
				//微信登录
				uni.login({
					provider: 'weixin',
					success: (res2) => {
						this.code = res2.code;
						console.log(this.code)

						if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户决绝授权
							//拒绝授权后弹出一些提示  
						} else { //允许授权  
							console.log(e.detail.encryptedData)
							console.log(e.detail.iv)
							// e.detail.encryptedData //加密的用户信息  
							// e.detail.iv //加密算法的初始向量  时要用到  
							this.appWechatLogin(this.code, e);
						}
					},
					fail: () => {
						uni.showToast({
							title: "微信登录授权失败",
							icon: "none"
						});
					}
				})
			},
			wechatLoginAndGoto(path) {
				// 判断是否过期
				uni.checkSession({
					success: function(res) {
						//一登陆
						uni.navigateTo({
							url: path
						});
					},
					fail: function(res) {
						// 已过期
						uni.navigateTo({
							url: '/pages/login/index?url=' + encodeURIComponent(path)
						});
						//uni.$u.route("/pages/login/index");
					},
				});
			},
			gouxuan() {
				if (!this.checked) {
					uni.showToast({
						title: "请先勾选协议",
						icon: "none"
					});
					return
				}
			},
			query() {
				//判断是否登录
				let vuex_token = this.$store.state.vuex_token;
				// if (!vuex_token) {
				// 	uni.navigateTo({
				// 		url: '/pages/login'
				// 	});
				// 	return
				// }
				if (!this.checked) {
					uni.showToast({
						title: "请先勾选协议",
						icon: "none"
					});
					return
				}
				if (this.communityId == -1) {
					if (this.checkFrom) {
						uni.showToast({
							title: "扫码进入才能查询",
							icon: "none"
						});
						return
					} else {
						this.communityId = 1;
					}
				}
				// this.$u.route("pages/healthqr");
				this.$u.route({
					url: 'pages/healthqr',
					params: {
						scene: this.communityId
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.title {
		text-align: center;
		margin-top: 92rpx;
		font-size: 60rpx;
		font-weight: bold;
		color: $u-font-blue-dark;
	}

	.mobile {
		text-align: center;
		font-size: 50rpx;
		color: #000000;
	}

	.sub-title {
		text-align: center;
		margin-top: 30rpx;
		font-size: 30rpx;
		font-weight: 500;
		color: $u-font-blue-dark;
	}

	.card-type {
		margin: auto;
		width: 500rpx;
		height: 434rpx;
	}

	.text-2 {
		text-align: center;
		font-size: 24rpx;
		font-weight: 500;
		color: #A8A8A8;
		line-height: 24rpx;
	}

	.text-3 {
		margin-top: 25rpx;
		text-align: center;
		font-size: 36rpx;
		font-weight: bold;
		color: #A8A8A8;
		line-height: 36rpx;
	}
</style>
